/*
 * @Description: 访问日志中间件
 * @Author: zheng yiliang
 * @Date: 2021-06-06 14:25:33
 * @LastEditTime: 2021-06-06 18:48:20
 * @LastEditors: zheng yiliang
 */

const axios = require('axios')
const { Visitlog } = require('../model');
var ipnet = require('xz-ipnet')();

const actions = {
  '/articlesgory': '访问博客列表',
  '/getArticleById': '访问文章详情',
  '/archivesQry': '访问归档',
  '/qq/login': 'qq第三方登录',
  '/qq/logout': '退出qq第三方登录',
  '/addComments': '评论文章',
  '/replyComments': '回复评论',
  '/addMessage': '添加留言',
  '/messages': '访问留言板',
  '/replyMessage': '回复留言',
  '/links': '访问友情链接',
  '/newArticlesQry': '访问首页'
}
module.exports = (req, res, next) => {
  const ip = req.headers['x-forwarded-for'] ||
    req.connection.remoteAddress ||
    req.socket.remoteAddress ||
    req.connection.socket.remoteAddress
  console.log('req:', req.url)
  const url = req.url.split('?')[0]
  const ipSource = ipnet.find(ip).filter(item => item).join('|')
  // ipnet
  const userAgent = getOSAndBrowser(req.headers['user-agent'])
  console.log('req.headers[user-agent]', req.headers['user-agent'])
  const plate = userAgent.split('---')[0]
  const browser = userAgent.split('---')[1]
  const visitor = new Visitlog({
    IP: ip,
    plate,
    IP_Source: ipSource,
    browser,
    actions: actions[url]
  })
  visitor.save().then(data => {
    next()
  })
}


function getOSAndBrowser (agent) {
  var str = agent.toLowerCase();
  var ver = str.match(/cpu (.*?) os (.*?) like mac os/);
  var agentID = agent.indexOf('Android');
  var os = agent;
  var userAgent = agent;
  console.log(userAgent);
  var info = "";
  var tempArray = "";
  if (agentID >= 0) {
    info += agent.slice(agentID, agentID + 22);
  } else if (ver) {
    console.log('5555555555555', ver)
    info += (ver[1] + ver[2].replace(/_/g, "."));
  } else {
    if (os.indexOf("Win") > -1) {
      if (userAgent.indexOf("Windows NT 5.0") > -1) {
        info += "Windows 2000";
      } else if (userAgent.indexOf("Windows NT 5.1") > -1) {
        info += "Windows XP";
      } else if (userAgent.indexOf("Windows NT 5.2") > -1) {
        info += "Windows 2003";
      } else if (userAgent.indexOf("Windows NT 6.0") > -1) {
        info += "Windows Vista";
      } else if (userAgent.indexOf("Windows NT 6.1") > -1 || userAgent.indexOf("Windows 7") > -1) {
        info += "Windows 7";
      } else if (userAgent.indexOf("Windows NT 6.2") > -1 || userAgent.indexOf("Windows NT 6.3") > -1 || userAgent.indexOf("Windows 8") > -1) {
        info += "Windows 8";
      } else if (userAgent.indexOf("Windows NT 6.4") > -1 || userAgent.indexOf("Windows NT 10") > -1) {
        info += "Windows 10";
      } else {
        info += "Other";
      }
    } else if (os.indexOf("Mac") > -1) {
      info += "Mac";
    } else if (os.indexOf("X11") > -1) {
      info += "Unix";
    } else if (os.indexOf("Linux") > -1) {
      info += "Linux";
    } else {
      info += "Other";
    }
  }

  info += "---";
  if (/[Ff]irefox(\/\d+\.\d+)/.test(userAgent)) {
    tempArray = /([Ff]irefox)\/(\d+\.\d+)/.exec(userAgent);
    info += tempArray[1] + tempArray[2];
  } else if (/[Tt]rident(\/\d+\.\d+)/.test(userAgent)) {
    tempArray = /([Tt]rident)\/(\d+\.\d+)/.exec(userAgent);
    if (tempArray[2] === "7.0") {
      tempArray[2] = "11";
    } else if (tempArray[2] === "6.0") {
      tempArray[2] = "10";
    } else if (tempArray[2] === "5.0") {
      tempArray[2] = "9";
    } else if (tempArray[2] === "4.0") {
      tempArray[2] = "8";
    }
    tempArray[1] = "IE";
    info += tempArray[1] + tempArray[2];
  } else if (/[Cc]hrome\/\d+/.test(userAgent)) {
    tempArray = /([Cc]hrome)\/(\d+)/.exec(userAgent);
    info += tempArray[1] + tempArray[2];
  } else if (/[Vv]ersion\/\d+\.\d+\.\d+(\.\d)* *[Ss]afari/.test(userAgent)) {
    tempArray = /[Vv]ersion\/(\d+\.\d+\.\d+)(\.\d)* *([Ss]afari)/.exec(userAgent);
    info += tempArray[3] + tempArray[1];
  } else if (/[Oo]pera.+[Vv]ersion\/\d+\.\d+/.test(userAgent)) {
    tempArray = /([Oo]pera).+[Vv]ersion\/(\d+)\.\d+/.exec(userAgent);
    info += tempArray[1] + tempArray[2];
  } else if (/MicroMessenger/.test(userAgent)) {
    info += 'MicroMessenger';
  }
  else {
    info += "unknown";
  }
  return info;
};